#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <time.h>

#include "room.h"
#define ROOM_FILE "room.txt"

int write_room(Room *r)
{
	FILE *fp;

	fp = fopen(ROOM_FILE,"a");
	if (fp == NULL)
	{
		perror("打开room.txt失败");
		return 1;
	}
	fprintf(fp,"%2d",r->roomId);
	fprintf(fp, "%-15s",r->name);
	fprintf(fp, "%5d",r->price);
	fprintf(fp, "%2d",r->discount);
	fprintf(fp, "%5d",r->cashPledge);
	fprintf(fp, "%2d\n",r->status);
	if (fp)
		fclose(fp);
	return 0;

}

int write_room_all(Room *r)
{
	FILE *fp;

	fp = fopen(ROOM_FILE,"w");
	if (fp == NULL)
	{
		perror("open room file");
		return 1;
	}

	while(r->next)
	{
		fprintf(fp,"%2d",r->roomId);
		fprintf(fp, "%-15s",r->name);
		fprintf(fp, "%5d",r->price);
		fprintf(fp, "%2d",r->discount);
		fprintf(fp, "%5d",r->cashPledge);
		fprintf(fp, "%2d\n",r->status);
		r = r->next;
	}
	if (fp) fclose(fp);

	return 0;
}


Room* read_room()
{
	Room *head,*p,*newNode;
	Room *tp;
	FILE *fp;
/*	if( access("ROOM_FILE",F_OK) != 0 )
	{
		pr = (Room*)malloc(sizeof(Room));
		if(pr == NULL)
		{
			return NULL;
		}
		memset(pr,0,sizeof(Room));
		printf("room.txt is Empty,please input the first room information:\n");
		printf("input the roomId:\n");
		scanf("%d",&pr->roomId);
		getchar();
		printf("input the name:\n");
		fgets(pr->name,15,stdin);
		printf("input the price:\n");
		scanf("%d",&pr->price);
		printf("input the discount:(0/1)\n");
		scanf("%d",&pr->discount);
		printf("input the cashPledge:\n");
		scanf("%d",&pr->cashPledge);
		printf("input the status:(0/1/2/3)\n");
		scanf("%d",&pr->status);

		write_room(pr);

		free(pr);
		
		return pr;
	}
*/
	fp = fopen (ROOM_FILE,"r");
	if (fp == NULL)
	{
		perror("open room file read");
		return NULL;
	}
	head = (Room *)malloc(sizeof(Room));
	if (NULL == head)
	{
		printf("malloc fail\n");
		if(fp) fclose(fp);
		return NULL;
	}
	memset(head,0,sizeof(Room));	
	p = head;
	while(!feof(fp))
	{
		fscanf(fp,"%2d",&p->roomId);
		fscanf(fp,"%s",p->name);
		fscanf(fp, "%5d",&p->price);
		fscanf(fp, "%2d",&p->discount);
		fscanf(fp, "%5d",&p->cashPledge);
		fscanf(fp, "%2d",&p->status);

//		printf("read one node\n");
//		printf("RoomId :%d\n",p->roomId);
//		printf("%s,%d ,%d, %d, %d",p->name,p->price,p->discount,p->cashPledge,p->status);

		newNode = (Room*)malloc(sizeof(Room));
		memset(newNode,0,sizeof(Room));
		p->next = newNode;
		tp = p;
		p = newNode;
	}
	tp->next = NULL;
	free(p);
	if(fp) fclose(fp);
	return head;
}

int display_room(Room *head)
{
	Room *p;
	int num = 1;
	p = head;
	printf("##########################\n");
	printf("Num  RoomId   Name           Price     Discount  CashPledge    Status\n");
	while(p->next)
	{	
		printf("%-2d.  %-2d       %-15s%-4d      %-d         %-4d          %-d  \n",num,p->roomId,p->name,p->price,p->discount,p->cashPledge,p->status);
		p = p->next;
		num++;
	}
	printf("##########################\n");
	return 0;
}

int add_room()
{
	Room *pr;
	pr = (Room*)malloc(sizeof(Room));
	if( pr == NULL )
	{
		printf("malloc fail\n");
		return 1;
	}
	memset(pr,0,sizeof(Room));	

	printf("请输入 roomId:\n");
	scanf("%d",&pr->roomId);
	getchar();

	printf("请输入 name:\n");
	fgets(pr->name,15,stdin);

	printf("请输入 price:\n");
	scanf("%d",&pr->price);
	printf("请输入 discount:(0/1)\n");
	scanf("%d",&pr->discount);
	printf("请输入 cashPledge:\n");
	scanf("%d",&pr->cashPledge);
	printf("请输入 status:(0/1/2/3)\n");
	scanf("%d",&pr->status);
	getchar();

	write_room(pr);

	free(pr);

	printf("增加客房成功!\n");
	printf("输入任意指令以继续:\n");
	getchar();	

	return 0;
}

int change_room()
{
	int i,choose;
	int id;
	Room *head,*p;
	head = read_room();
	display_room(head);
	p = head;
	printf("输入 roomId 以改变房间信息:\n");
	scanf("%d",&id);
	while(p->next != NULL)
	{
		
		if (id == p->roomId)
			break;
		p = p->next;
	}
	getchar();
	
	if (p->next == NULL)
	{
		printf("not find the roomId!\n");
		printf("输入任意指令以继续:\n");
		getchar();
		return 0;
	}
	printf("请输入你想改变的信息对应的编号:\n ");
	printf("1.name  2.price  3.discount  4.cashPledge  5.status 0.(quit)\n");
	scanf("%d",&i);
	getchar();
	switch(i)
	{
		case 1:
			printf("请输入 new name:\n");
			fgets(p->name,15,stdin);
			break;
		case 2:
			printf("请输入 new price:\n");
			scanf("%d",&p->price);
			getchar();
			break;
		case 3:
			if ( p->discount == 0)
			{
				printf("now discount is 0 ,change to 1 ?\n");
				printf("input 1/0 for yes/no \n");
				scanf("%d",&choose);
				getchar();
				if (choose == 1)
					p->discount = 1;

			}
			else if (p->discount == 1)
			{
				printf("now sex is 1 ,change to 1 ?\n");
				printf("input 1/0 for yes/no \n");
				scanf("%d",&choose);
				getchar();
				if (choose == 1)
					p->discount = 0;
			}
			break;
		case 4:
			printf("请输入 new cashPledge:\n");
			scanf("%d",&p->cashPledge);
			getchar();
			break;
		case 5:
			printf("请输入  new status:(0/1/2/3)\n");
			scanf("%d",&p->status);
			getchar();
		case 0:
		default:
			break;
	}

	write_room_all(head);

	printf("修改客房信息成功!\n");
	printf("输入任意指令以继续:\n");
	getchar();
	return 0;
}

int remove_room()
{

	int s,i;    //s for remove the num node 
	Room *remove_head,*remove_p,*remove_tp;
	remove_head = read_room();
	display_room(remove_head);
	remove_p = remove_tp = remove_head;
	remove_p = remove_p->next;
	printf("请输入要删除的房间对应的编号 :\n");
	scanf("%d",&s);	
	getchar();
	for (i = 2;i < s;i++)
	{
		remove_p = remove_p->next;
		remove_tp = remove_tp->next;
	}

	remove_tp->next = remove_p->next;   //to remove
	free (remove_p);

	write_room_all(remove_head);

	printf("成功删除客房\n");
	printf("输入任意指令以继续:\n");
	getchar();

	return 0;
}

int display_nclean_room(Room *head)
{
	Room *p;
	int num = 1;
	p = head;
	while(p->next)
	{	
		if(p->status == 2)
		{	
			break;
		}
		p = p->next;
	}
	if(p->next == NULL)
	{
		printf("所有房间都已经打扫完成！\n");
		return 1;
	}
	printf("##########################\n");
	printf("Num  RoomId   Name           Status\n");
	while(p->next)
	{	
		if(p->status == 2)
		{	
			printf("%-2d.  %-2d       %-15s%-d  \n",num,p->roomId,p->name,p->status);
			num++;
		}
		p = p->next;
	}
	printf("##########################\n");
	return 0;
}

void confirm_room_clean()
{
    Room *p,*head; 
    int roomid;	
	head = read_room();
	p = head;
	if(display_nclean_room(p) == 1)
		return ;
	printf("请输入确认打扫完成的 roomID:\n");
	scanf("%d",&roomid);	
	getchar();
	while( p->next != NULL )
	{
		if(p->roomId == roomid)
		{
			p->status = 0;
			break;
		}
		p = p->next;
	}
	
	if(p->next == NULL)
	{
		printf("没有找到输入的roomId\n");
		return ;
	}
	write_room_all(head);

	return ;
}

int recept_find_room()
{
	Room *head,*p;
	head = read_room();
//	display_room(head);
	p = head;
	int num = 1;
	while(p->next)
	{	
		if(p->status == 0)
		{	
			break;
		}
		p = p->next;
	}
	if(p->next == NULL)
	{
		printf("无空房！\n");
		return 1;
	}
	printf("##########################\n");
	printf("Num  RoomId   Name           Price    Status\n");
	while(p->next)
	{	
		if(p->status == 0)
		{	
			printf("%-2d.  %-2d       %-15s%-4d     %-d  \n",num,p->roomId,p->name,p->price,p->status);
			num++;
		}
		p = p->next;
	}
	printf("##########################\n");

	
//	printf("输入任意指令以继续:\n");
//	getchar();
	return 0;

}	


int check_in()
{
	Room *head,*p;
	int s,loop = 1;
	head = read_room();	
	p = head;
	while(loop)
	{	
		printf("请输入入住的房间号:\n");
		scanf("%d",&s);	
		getchar();

		while(p->next)
		{
			if(s == p->roomId && p->status == 0)
			{
				loop = 0;
			//	printf("find\n");
				break;
			}
			p = p->next;
		}
		if(!p->next)
		{
			printf("没有找到此房间号\n");
			return 1;			
		}
	}
	p->status = 1;
	write_room_all(head);

//	printf("输入任意指令以继续:\n");
//	getchar();
	return 0;
}
 int order_in()
 {
	Room *head,*p;
	int s,loop = 1;
	head = read_room();	
	p = head;
	while(loop)
	{	
		printf("请输入预约的房间号:\n");
		scanf("%d",&s);	
		getchar();

		while(p->next)
		{
			if(s == p->roomId && p->status == 0)
			{
				loop = 0;
			//	printf("find\n");
				break;
			}
			p = p->next;
		}
		if(!p->next)
		{
			printf("没有找到此房间号\n");	
			return 1;			
		}
	}
	p->status = 1;
	write_room_all(head);

//	printf("输入任意指令以继续:\n");
//	getchar();
	return 0;
}

int free_room()
{
	Room *head,*p;
	int s;
	head = read_room();
	p = head;

	printf("请输入退房的房间号:\n");
	scanf("%d",&s);	
	getchar();

	while(p->next)
	{
		if(s == p->roomId && p->status == 1)
		{
		//	printf("find\n");
			break;
		}
		p = p->next;
	}
	if(!p->next)
	{
		printf("没有找到此房间号\n");	
		return 1;			
	}

	p->status = 2;
	printf("退房成功！\n");
	write_room_all(head);

//	printf("输入任意指令以继续:\n");
//	getchar();
	return 0;

}

int exchange_room()
{
	recept_find_room();
	Room *head,*p;
	int s;
	head = read_room();
	p = head;

	printf("请输入换房的原房间号:\n");
	scanf("%d",&s);	
	getchar();

	while(p->next)
	{
		if(s == p->roomId && p->status == 1)
		{
		//	printf("find\n");
			break;
		}
		p = p->next;
	}
	if(!p->next)
	{
		printf("没有找到此房间号\n");	
		return 1;			
	}
	p->status = 2;
	printf("\n搜索到该房间信息:\n");
	printf("Num  RoomId   Name           Price    Status\n");
	printf("1 .  %-2d       %-15s%-4d     %-d  \n",p->roomId,p->name,p->price,p->status);
	

	p = head;
	printf("请输入要调换的房间号:\n");
	scanf("%d",&s);	
	getchar();
	while(p->next)
	{
		if(s == p->roomId && p->status == 0)
		{		
			break;
		}
		p = p->next;
	}
	if(!p->next)
	{
		printf("没有找到此房间号\n");
		return 1;			
	}
	p->status = 1;

	write_room_all(head);
	return 0;

}

int keep_room()
{

	Room *head,*p;
	int s;
	head = read_room();
	p = head;

	printf("请输入续住的房间号:\n");
	scanf("%d",&s);	
	getchar();

	while(p->next)
	{
		if(s == p->roomId && p->status == 1)
		{
		//	printf("find\n");
			break;
		}
		p = p->next;
	}
	if(!p->next)
	{
		printf("没有找到此房间号\n");	
		return 1;			
	}
	printf("搜索到该房间信息:\n");
	printf("Num  RoomId   Name           Price    Status\n");
	printf("1 .  %-2d       %-15s%-4d     %-d  \n",p->roomId,p->name,p->price,p->status);

	return 0;

}